package com.emenu.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class RSStrFromJsonText {

	// 写sd卡
	public static void saveFileToSDCard(String filename, String content)
			throws Exception {
		Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
		// File file = new File("/sdcard", filename);
		String SDPATH = Environment.getExternalStorageDirectory()
				+ File.separator + "jsontext" + File.separator;

		File destDir = new File(SDPATH);
		if (!destDir.exists()) {
			destDir.mkdirs();
		}
		File f = new File(SDPATH + filename);
				
		try {
			//openFileOutput方式文件会被保存在/data/data/应用名称/files/下
			//FileOutputStream outputStream = openFileOutput(filename,  
			//Activity.MODE_PRIVATE); 
			//数据流写入方式,默认覆盖，new FileOutputStream(f,true) 追加
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(content.getBytes());		 
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 读取sd卡jsontext
	 * 返回string
	 * @param InputStream
	 * @param contentString
	 * @throws Exception
	 * return string
	 */
	public static  String readDate ( String filename) throws Exception
	{		
		String SDPATH = Environment.getExternalStorageDirectory()
		+ File.separator + "jsontext" + File.separator;
		File f = new File(SDPATH + filename);
		
		FileInputStream inputStream = new FileInputStream(f);

		byte [] byte1 = new byte[1024*1024];
		/**
		 * 当输入流读到文件的末尾 返回就是－1 
		 */
		int length = inputStream.read(byte1);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		if(length!=-1)
		{
			//读到的内容存在内存中ByteArrayOutputStream 这个类用于将byte流存储在内存中
			
			byteArrayOutputStream.write(byte1, 0, length);
		}
		String dateString =   byteArrayOutputStream.toString();
		byteArrayOutputStream.close();
		inputStream.close();
		return dateString;
	}
	
	// 写文件，放内存/data/下
	public static void write(Context context, String file, String msg) {
		if (msg == null) {
			
			return;
		}
		
		FileOutputStream stream = null;
		String writemsg = null;
		try {
			stream = context.openFileOutput(file, Context.MODE_WORLD_WRITEABLE
					| Context.MODE_PRIVATE);
		
			byte b[] = msg.getBytes();
			writemsg = new String(b, "UTF-8");
			stream.write(writemsg.getBytes());

		} catch (FileNotFoundException e) {
		
		} catch (IOException e) {
			
		} finally {
			
			if (stream != null) {
				
				try {
					stream.flush();
					stream.close();
				} catch (IOException e) {

				}
			}
		}
	}
	
	// 读文件,获得assets文件夹下问件
	public String strJson(Context context,String jsonFile) {
		String content = "";
		InputStream is = null;
		try {
			is = context.getAssets().open(jsonFile);
			byte[] data = new byte[is.available()];
			is.read(data);
			content = new String(data);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (Exception ie) {
				ie.printStackTrace();
			}
		}
		return content;
	}
}
